PATHMac OS 8 and 9 Developer Documentation > Interapplication Communication > AppleScript for Scripters >

AppleScript Language Guide

   

Working With Unit Type Value Classes

This section provides sample script statements for working with Unit Type value classes.

The following statements calculate the area of a circle with a radius of 7 yards, then coerce the area to square feet ( pi is an AppleScript-defined constant).

set circleArea to (pi * 7) as square yards
    --result: square yards 21.991148575129
circleArea as square feet
    --result: square feet 197.920337176157

The following statements set a variable to a value of 5.0 square kilometers, then coerce it to various other units of area:

set theArea to square kilometers 5.0
    --result: square kilometers 5.0
theArea as square miles
    --result: square miles 1.930510798581
theArea as square meters
    --result: square meters 5.0E+6

You can also coerce a value from square meters to a real number or an integer:

set theArea to square meters 5.0
    --result: square meters 5.0
theArea as real
    --result: 5.0
theArea as integer
    --result: 5

However, you cannot coerce an area measurement to a Unit Type in a different category:

set theArea to square meters 5.0
    --result: square meters 5.0
theArea as cubic meters
    --result: error
theArea as degrees Celsius
    --result: error

The following statements demonstrate coercion of a Unit Type to a String, and from a String to a Unit Type:

set myValue to pounds 2.2   --result: pounds 2.2
myValue as string           --result: "2.2"
"2.2" as kilograms          --result: kilograms 2.2

© 1999 Apple Computer, Inc. – (Last Updated 21 May 99)